package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.IeoRule;
import com.novax.ex.activity.open.api.inner.IeoInnerRuleApi;
import com.novax.ex.activity.open.model.request.IeoRuleRequest;
import com.novax.ex.activity.open.model.response.IeoRuleResponse;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.IeoRuleService;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.*;

/**
 * @Description 活动规则Controller
 * @Author Cody
 * @Date 2022/12/05/10:44
 */
@Slf4j
@RestController
public class IeoRuleController extends BaseActivityController implements IeoInnerRuleApi {
    @Resource
    IeoRuleService ieoRuleService;

    @Override
    public ReturnResult<List<IeoRuleResponse>> findByIeoId(Long ieoId) {
        List<IeoRule> byIeoId = ieoRuleService.findByIeoId(ieoId);
        List<IeoRuleResponse> ieoRuleResponses = CopyUtils.copyList(byIeoId, IeoRuleResponse.class);
        return ReturnResult.success(ieoRuleResponses);
    }

    @Override
    public ReturnResult add(String language, IeoRuleRequest ruleRequest) {
        if(Objects.isNull(ruleRequest.getIeoId())){
            return ReturnResult.fail(InternationalCode.LACK_VALUE);
        }
        IeoRule ruleByIeoIdAndLanguage = ieoRuleService.findRuleByIeoIdAndLanguage(
                ruleRequest.getIeoId(), ruleRequest.getLanguage());
        if (Objects.nonNull(ruleByIeoIdAndLanguage)) {
            return ReturnResult.fail(getInternational(language, InternationalCode.ADMIN_IDENTICAL_DATA));
        }
        ruleRequest.setCreateTime(new Date());
        boolean res = ieoRuleService.addSelective(ruleRequest);
        return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
                : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult modify(String language, Long id, IeoRuleRequest ruleRequest) {
        if(Objects.isNull(ruleRequest.getIeoId())){
            return ReturnResult.fail(InternationalCode.LACK_VALUE);
        }
        ruleRequest.setId(id);
        ruleRequest.setUpdateTime(new Date());
        boolean res = ieoRuleService.modifySelective(ruleRequest);
        return res ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS))
                : ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }
}
